package com.tg.base.concurrent.future;

import java.util.concurrent.*;

/**
 * 测试多个线程进入get方法阻塞时会出现什么情况
 */
public class FutureTaskGetBlockTest {
    public static void main(String[] args) throws Exception {
        final FutureTask<String> futureTask1 = new FutureTask<String>(new GetBlock(9)) {
            @Override
            protected void done() {
                System.out.println("任务完成x");
                super.done();
            }
        };

        ExecutorService executor = Executors.newFixedThreadPool(4);
        executor.submit(futureTask1);
        executor.submit(futureTask1);

        TimeUnit.MILLISECONDS.sleep(200);

        executor.execute(() -> {
            try {
                String s = futureTask1.get();
                System.out.println("结果1：" + s);
            } catch (ExecutionException | InterruptedException ex) {
                ex.printStackTrace();
            }
        });
        executor.execute(() -> {
            try {
                String s = futureTask1.get();
                System.out.println("结果2：" + s);
            } catch (ExecutionException | InterruptedException ex) {
                ex.printStackTrace();
            }
        });

        executor.shutdown();
    }


    private static void threadSleep(long ms) {
        try {
            TimeUnit.MILLISECONDS.sleep(ms);
        } catch (InterruptedException ex) {

        }
    }
}
